package com.sojson.project.monitor.job.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.annotation.Log;
import com.sojson.config.exception.TransErrorCode;
import com.sojson.enums.core.EBusinessType;
import com.sojson.project.monitor.job.controller.param.verify.SysJobParamVerify;
import com.sojson.project.monitor.job.entity.dto.JobDto;
import com.sojson.project.monitor.job.entity.dto.JobPageDto;
import com.sojson.project.monitor.job.entity.po.Job;
import com.sojson.project.monitor.job.entity.vo.JobVo;
import com.sojson.project.monitor.job.service.JobService;
import com.sojson.result.Result;
import com.sojson.util.poi.ExcelUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 定时任务调度表控制器
 * 
 * @author liu
 * @date 2022-10-24
 */
@Api(tags = "定时任务调度表管理")
@RestController
@RequestMapping("/monitor/job")
public class JobController {

    @Autowired
    private JobService jobService;

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('/monitor/job/save')")
    @ApiOperation("给定时任务调度表添加一条数据")
    public Result<Boolean> save(@RequestBody Job dto) throws Exception {
        // 校验参数
        Result<Boolean> paramResult = SysJobParamVerify.save(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dto.insertInit();
        return Result.success(jobService.add(dto));
    }

    /**
     * 通过id更新
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/monitor/job/updateById')")
    @ApiOperation("通过id更新定时任务调度表数据")
    public Result<Boolean> updateById(@RequestBody Job dto) throws Exception {
        // 校验参数
        Result<Boolean> paramResult = SysJobParamVerify.updateById(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(jobService.update(dto));
    }

    /**
     * 列表查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('/monitor/job/list')") 
    @ApiOperation(value = "查询定时任务调度表数据", response = JobVo.class)
    public Result<List<JobVo>> list(JobDto dto) {
        return Result.success(jobService.list(dto));
    }

    /**
     * 分页查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermi('/monitor/job/page')") 
    @ApiOperation(value = "分页查询定时任务调度表数据", response = JobVo.class)
    public Result<List<JobVo>> page(JobPageDto dto) {
        return Result.success(jobService.page(dto));
    }

    /**
     * 导出数据
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermi('/monitor/job/export')")
    @ApiOperation("导出定时任务调度表数据")
    public void export(JobDto dto) throws Exception {
        List<JobVo> list = jobService.list(dto);
        ExcelUtil<JobVo> excelUtil = new ExcelUtil<>(JobVo.class);
        excelUtil.exportExcelOutFile(list, "定时任务调度表");
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPermi('/monitor/job/info/{id}')")
    @ApiOperation(value = "通过id查询定时任务调度表数据", response = JobVo.class)
    public Result<JobVo> info(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(jobService.findById(id));
    }

    /**
     * 通过id列表删除
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @DeleteMapping("/remove")
    @PreAuthorize("@ss.hasPermi('/monitor/job/remove')")
    @ApiOperation("通过id列表删除定时任务调度表数据")
    public Result<Boolean> remove(@RequestBody JobDto dto) throws Exception {
        List<String> ids = dto.getIds();
        if (ids == null || ids.size() == 0) {
            return Result.success();
        }

        return Result.success(jobService.removeByIds(ids));
    }

    /**
     * 清空数据
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/monitor/job/clear')")
    @ApiOperation("清空定时任务调度表数据")
    public Result<Boolean> clear() {
        return Result.success(jobService.clear());
    }

    /**
     * 通过id删除
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("@ss.hasPermi('/monitor/job/remove/{id}')")
    @ApiOperation("通过id删除定时任务调度表数据")
    public Result<Boolean> remove(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(jobService.removeById(id));
    }

    /**
     * 通过id修改状态
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @PutMapping("/changeStatus")
    @PreAuthorize("@ss.hasPermi('/monitor/job/changeStatus')")
    public Object updateStatusById(@RequestBody Job dto) throws Exception {
        // 校验参数
        Result<Boolean> paramResult = SysJobParamVerify.updateStatusById(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        Job job = new Job();
        job.setId(dto.getId());
        job.setStatus(dto.getStatus());
        jobService.updateStatusById(job);
        return Result.success();
    }

    /**
     * 定时任务立即执行一次
     * 
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(title = "定时任务", businessType = EBusinessType.UPDATE)
    @PutMapping("/run")
    @PreAuthorize("@ss.hasPermi('/monitor/job/run')")
    public Result<Integer> run(@RequestBody Job dto) throws Exception {
        jobService.run(dto);
        return Result.success();
    }

}